package cn.lesaas.esb.poc.test;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

public class BusRoute extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		errorHandler(deadLetterChannel("seda:error"));

		// 提供TCP访问
		from("netty4:tcp://172.16.100.41:9000?allowDefaultCodec=false&encoder=#stringEncoder&decoder=#stringDecoder").startupOrder(30).choice()
		.when().xpath("/reqt/header/id='01'")
			.to("seda:dealy1")
			.process("respProcessor")
		.when().xpath("/reqt/header/id='02'")
			.to("seda:dealy2")
			.process("respProcessor")
		.otherwise()
			.to("netty4:tcp://172.16.100.41:59999?allowDefaultCodec=false&encoder=#stringEncoder&decoder=#stringDecoder");
		// 提供webservice访问
		//from("cxf:");

		// 模块划分
		// xml消息解码
		//from("direct:msg").to("direct:busi");
		// xml消息编码
		
		//实时交易
		
		// 延迟执行 5秒 发送到核心业务系统
		//TODO 2小时延时执行后，可能业务系统不在运营时间
		from("seda:dealy1").delay(1000 * 5).to("netty4:tcp://172.16.100.41:59999?allowDefaultCodec=false&encoder=#stringEncoder&decoder=#stringDecoder")
			.choice()
				.when().xpath("/resp/header/result='FAIL'")
					.to("direct:冲正")
				.otherwise()
					.to("log:SUC?showAll=true");
		
		//隔日执行
		// 24小时以后执行
		from("seda:dealy2").delay(1000 * 5).to("netty4:tcp://172.16.100.41:59999?allowDefaultCodec=false&encoder=#stringEncoder&decoder=#stringDecoder")
			.choice()
				.when().xpath("/resp/header/result='FAIL'")
					.to("direct:冲正")
				.otherwise()
					.to("log:SUC?showAll=true");
		
		
		//失败冲正交易 
		from("direct:冲正").process(new Processor() {
			@Override
			public void process(Exchange exchange) throws Exception {
				System.out.println("冲正");
			}
		});
		

		// 错误处理 记录数据库等等
		//from("seda:erro").to("sql");
	}

}
